import Mock from "mockjs";

// 首页数据
let dataList = Mock.mock({
  // 20条数据
  "data|50": [
    {
      // 商品Id
      "goodsId|+1": 10000,
      // name
      name: "@cname",
      //市
      ccity: "@city",
      //县
      country: "@county",
      //省 + 市
      ss: "@city(true)",
      //省市县
      countrysx: "@county(true)",
      //商品售价
      "goodsSale|30-500": 30,
    },
  ],
});

// 全部订单数据
let DiDanList = Mock.mock({
  // 20条数据
  "data|100": [
    {
      // 商品Id
      "goodsId|+1": 5154795400,
      // name
      name: "@cname",
      //商品地址
      goodsAddress: "@county(true)",
      //商品售价
      "goodsSale|30-500": 30,
      // 邮政编码
      code: "@zip",
    },
  ],
});

// 优惠券数据
let YhList = Mock.mock({
  // 20条数据
  "data|100": [
    {
      // 商品Id
      "goodsId|+1": 1,
      // name
      name: "@cname",
      //商品名称
      goodsName: "@ctitle(3)",
      //开始时间
      created: "@date('2022年5月20日')",
      //结束时间
      createdend: "@date('2022年6月18日')",
      //商品售价 秒杀价格
      "goodsSale1|100-900": 30,
      //商品售价 现价
      "goodsSale2|900-2000": 30,
      //数量
      "goodsSale3|900-20000": 500,
    },
  ],
});

// 全部商品数据
// 定义数据类型
var dityData = Mock.mock({
  // 20条数据
  "data|195": [
    {
      // 商品Id
      "goodsId|+1": 1,
      // 供应商名字
      name: "@cname",
      //商品名称
      goodsName: "@ctitle(3)",
      //邮政编码
      code: "@zip",
      //商品售价
      "goodsSale|8000-15000": 8000,
    },
  ],
});

// 对象类型
interface ParamsType {
  page: number;
  size: number;
}
interface ParamsTypeId {
  page: number;
  size: number;
  id: number;
}
// 对象类型
interface ParamsID {
  id: number;
}

class Data {
  // 首页数据
  getData({ page, size }: ParamsType) {
    return new Promise((resolved) => {
      const result = dataList.data.slice((page - 1) * size, page * size);
      resolved({
        status: 0,
        result: result,
        Total: dataList.data.length,
      });
    });
  }

  // 全部订单数据
  cityData({ page, size }: ParamsType) {
    return new Promise((resolved) => {
      const result = DiDanList.data.slice((page - 1) * size, page * size);
      resolved({
        status: 0,
        result: result,
        Total: DiDanList.data.length,
      });
    });
  }

  // 查询数据
  CxiDS({ id }: ParamsID) {
    return new Promise((resolved) => {
      const result = DiDanList.data.filter((item: any) => item.goodsId === id);
      console.log(result);
      resolved({
        status: 0,
        result: result,
      });
    });
  }

  // 优惠券数据
  YhData({ page, size }: ParamsType) {
    return new Promise((resolved) => {
      const result = YhList.data.slice((page - 1) * size, page * size);
      resolved({
        status: 0,
        result: result,
        Total: YhList.data.length,
      });
    });
  }

  // 全部商品数据
  AllitemsData({ page, size }: ParamsType) {
    return new Promise((resolved) => {
      const result = dityData.data.slice((page - 1) * size, page * size);
      resolved({
        status: 0,
        result: result,
        Total: dityData.data.length,
      });
    });
  }
  // 全部商品数据
  AllitemsData2({ id }: ParamsID) {
    return new Promise((resolved) => {
      const result = dityData.data.filter((item: any) => item.goodsId == id);

      resolved({
        status: 0,
        result: result,
      });
    });
  }

  // 删除商品数据
  removedData({ page, size, id }: ParamsTypeId) {
    return new Promise((resolved) => {
      let result = dityData.data.filter((item: any) => item.goodsId !== id);

      result = result.slice((page - 1) * size, page * size);
      resolved({
        status: 0,
        result: result,
      });
    });
  }

  // 商品查询接口
  queryData({ id }: ParamsID) {
    return new Promise((resolved) => {
      const result = dityData.data.filter((item: any) => item.name === id);
      resolved({
        status: 0,
        result: result,
      });
    });
  }
  // 修改接口
  ModifyData(data: any) {
    console.log(data);
    return new Promise((resolved) => {
      let result = dityData.data.filter(
        (item: any) => item.goodsId === data.id
      );
      result = data;
      console.log(result);

      dityData.data.splice(data.id, 1, result);

      resolved({
        status: 0,
        result: dityData.data,
      });
    });
  }
}

const data = new Data();
export default data;
